/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.mobiubiq.directory.client.data;

import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Date;
import javax.microedition.rms.InvalidRecordIDException;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreNotOpenException;
import org.kalmeo.kuix.core.model.DataProvider;
import org.mobiubiq.directory.client.object.AbstractObject;
import org.mobiubiq.directory.client.object.BarcodeObject;
import org.mobiubiq.directory.client.object.LocationObject;
import org.mobiubiq.directory.client.object.ObjectType;
import org.mobiubiq.directory.client.object.TagObject;
import org.mobiubiq.directory.client.object.io.ObjectReaderWriter;
import org.mobiubiq.directory.client.object.io.ObjectReaderWriterFactory;

/**
 *
 * @author Marnix Renne
 */
public class Bookmark extends DataProvider {
    public static final String TYPE_PROPERTY = "type";
    public static final String TITLE_PROPERTY = "title";
    public static final String DATE_PROPERTY = "date";
    public static final String BARCODE_PROPERTY = "barcode";
    public static final String TAG_PROPERTY = "tag";
    public static final String LATITUDE_PROPERTY = "latitude";
    public static final String LONGITUDE_PROPERTY = "longitude";

    private String title;
    private Date date;
    private int recordId = -1;
    private AbstractObject object;
    
    public Bookmark(AbstractObject object) {
        this.object = object;
        this.title = object.getTitle();
        date = new Date();
    }
    
    public Bookmark(int recordId, DataInputStream das) throws RecordStoreNotOpenException, InvalidRecordIDException, RecordStoreException, IOException {
        super();
        this.recordId = recordId;
        ObjectType type = ObjectType.get(das.readUTF());
        title = das.readUTF();
        date = new Date(das.readLong());
        ObjectReaderWriter rw = ObjectReaderWriterFactory.get(type);
        object = rw.read(das);
    }
    
    public void setTitle(String value) {
        title = value;
        dispatchUpdateEvent(TITLE_PROPERTY);
    }
    
    public String getTitle() {
        return title;
    }
    
    public Date getDate() {
        return date;
    }
    
    public AbstractObject getObject() {
        return object;
    }
    
    public byte[] toByteArray() throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(baos);
        dos.writeUTF(object.getType().toString());
        dos.writeUTF(title);
        dos.writeLong(date.getTime());
        ObjectReaderWriter rw = ObjectReaderWriterFactory.get(object.getType());
        rw.write(object, dos);
        return baos.toByteArray();
    }
    
    protected Object getUserDefinedValue(String property) {
        if(TYPE_PROPERTY.equals(property)) {
            return object.getType().toString();
        }
        if(TITLE_PROPERTY.equals(property)) {
            return title;
        }
        if(DATE_PROPERTY.equals(property)) {
            return date.toString();
        }
        if(LATITUDE_PROPERTY.equals(property)) {
            return String.valueOf(((LocationObject)object).getLatitude());
        }
        if(LONGITUDE_PROPERTY.equals(property)) {
            return String.valueOf(((LocationObject)object).getLongitude());
        }
        if(BARCODE_PROPERTY.equals(property)) {
            return ((BarcodeObject)object).getBarcode();
        }
        if(TAG_PROPERTY.equals(property)) {
            return ((TagObject)object).getTag();
        }
        return null;
    }
    
    public int getRecordId() {
        return recordId;
    }
    
    public void setRecordId(int id) {
        recordId = id;
    }
    
}
